﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chinaer.Core.Helper
{
    /// <summary>
    /// 单一实例  整个程序中只存在唯一示例
    /// </summary>
    public class ChinaerInfoManager
    {
        private ChinaerInfoManager() { }
        private static ChinaerInfoManager _instance = null;
        private string _filePath = null;
        public string FilePath
        {
            get
            {
                return _filePath;
            }
            set
            {
                _filePath = value;
            }
        }
        /// <summary>
        /// 所有orm sql 列表 不可更改
        /// </summary>
        public static ChinaerInfoManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    try
                    {
                        _instance = new ChinaerInfoManager();
                    }
                    catch (Exception ex)
                    {
                        Chinaer.Core.Helper.Mobile.JsonHelper.GetJsonResult(false, ex.Message);
                    }
                }
                return _instance;
            }
            private set { }
        }
        private List<Model.Orm.Model_OrmStatement> _listOrmStatement = null;
        /// <summary>
        /// 属性 表示的是orm文件中所有的sql列表
        /// </summary>
        public List<Model.Orm.Model_OrmStatement> ListOrmStatements
        {
            get
            {
                if (_listOrmStatement == null)
                {
                    _listOrmStatement = GetOrmStatements();
                }
                return _listOrmStatement;
            }
        }
        /// <summary>
        /// 获取所有的sql列表
        /// </summary>
        /// <returns></returns>
        private List<Model.Orm.Model_OrmStatement> GetOrmStatements()
        {

            OrmXmlHelper ormXmlHelper = new OrmXmlHelper();
            if (!string.IsNullOrEmpty(FilePath))
            {
                ormXmlHelper = new OrmXmlHelper(FilePath);
            }
            return ormXmlHelper.GetSqlByXml();
        }
        /// <summary>
        /// 根据statementName获取其中的实体
        /// </summary>
        /// <param name="statementName"></param>
        /// <returns></returns>
        public Model.Orm.Model_OrmStatement GetModelInOrmStatementsByStatementName(string statementName)
        { 
            Model.Orm.Model_OrmStatement ormStatement=null;
            foreach (var item in ListOrmStatements)
            {
                if (item.StatementName.Equals(statementName, StringComparison.OrdinalIgnoreCase))
                {
                    ormStatement = item;
                    break;
                }
            }
            return ormStatement;
        }
        /// <summary>
        /// 根据stateMentName获取对应的sql语句
        /// </summary>
        /// <param name="statementName"></param>
        /// <returns></returns>
        public string GetSqlInOrmStatementsByStatementName(string statementName)
        {
            string sql = null;
            Model.Orm.Model_OrmStatement ormStatement = GetModelInOrmStatementsByStatementName(statementName);
            if (ormStatement != null)
            {
                sql = ormStatement.Statement;
            }
            return sql;
        }

    }
}
